package com.mDigitalHealth.thread;

import java.util.Calendar;

import com.mDigitalHealth.common.Utils;
import com.mDigitalHealth.database.DataType;
import com.mDigitalHealth.database.DatabaseHelper;
import com.mDigitalHealth.database.Store;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.preference.PreferenceManager;

public class HourSaveThread extends Thread {

	Context context;
	SharedPreferences statePreference;
	SharedPreferences.Editor stateEditor;
	int todaySteps = 0;
	
	public HourSaveThread(Context context, SharedPreferences statePreference, SharedPreferences.Editor stateEditor,
			int todaySteps){
		this.context = context;
		this.statePreference = statePreference;
		this.stateEditor = stateEditor;
		this.todaySteps = todaySteps;
		System.out.println("***hour save thead construct");
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		
		System.out.println("****hour save thread run");
		SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
		String name = mSharedPreferences.getString(Utils.PREFERENCE_KEY_USER_NAME, "");
		int steps = 0;
		Calendar mCalendarNow = Calendar.getInstance();
		DatabaseHelper mDatabaseHelper = new DatabaseHelper(context);
		if(mCalendarNow.get(Calendar.HOUR_OF_DAY) > 2){
			mCalendarNow.set(Calendar.HOUR_OF_DAY, 1);
			mCalendarNow.set(Calendar.MINUTE, 1);
			Cursor mcursor = mDatabaseHelper.query(DataType.HOUR_DATA, null, Store.HourDataColumns.HOUR_STAMP + " > ? AND "+Store.HourDataColumns.HOUR_STAMP + " < ? AND "+Store.HourDataColumns.LOGIN_USER+ " = ? ", 
												new String[]{String.valueOf(mCalendarNow.getTimeInMillis()),String.valueOf(Calendar.getInstance().getTimeInMillis()), name}, 
												null);
			if(mcursor != null){
				System.out.println(" > 2 and cursor count="+mcursor.getCount());
				mcursor.moveToFirst();
				while(!mcursor.isAfterLast()){
					steps += mcursor.getInt(mcursor.getColumnIndex(Store.HourDataColumns.HOUR_STEP_COUNT_TOTAL));
					mcursor.moveToNext();
				}
				mcursor.close();
				mcursor = null;
			}
		} else{
			mCalendarNow.add(Calendar.DAY_OF_MONTH, -1);
			Cursor mcursor = mDatabaseHelper.query(DataType.HOUR_DATA, null, Store.HourDataColumns.HOUR_STAMP + " > ? AND "+Store.HourDataColumns.HOUR_STAMP + " < ?  AND "+Store.HourDataColumns.LOGIN_USER + " = ? ", 
					new String[]{String.valueOf(mCalendarNow.getTimeInMillis()),String.valueOf(Calendar.getInstance().getTimeInMillis()),name}, 
					null);
			if(mcursor != null){
				System.out.println(" < 2 and cursor count="+mcursor.getCount());
				mcursor.moveToFirst();
				while(!mcursor.isAfterLast()){
				steps += mcursor.getInt(mcursor.getColumnIndex(Store.HourDataColumns.HOUR_STEP_COUNT_TOTAL));
				mcursor.moveToNext();
				}
				mcursor.close();
				mcursor = null;
			}
		} 
		System.out.println("**today steps="+todaySteps+",steps="+steps);
		ContentValues values = new ContentValues();
		values.put(Store.HourDataColumns.HOUR_STAMP, statePreference.getLong("hour_time", Calendar.getInstance().getTimeInMillis() - 3600 * 1000));
		values.put(Store.HourDataColumns.HOUR_STEP_COUNT_TOTAL, Math.max(0, todaySteps - steps));
		values.put(Store.HourDataColumns.HOUR_STEP_COUNT_HIGH, 0);
		values.put(Store.HourDataColumns.HOUR_STEP_COUNT_MID, Math.max(0, todaySteps - steps));
		values.put(Store.HourDataColumns.HOUR_STEP_COUNT_LOW, 0);
		values.put(Store.HourDataColumns.LOGIN_USER, name);
		mDatabaseHelper.insert(DataType.HOUR_DATA, values);
		mDatabaseHelper.close();
		mDatabaseHelper = null;
		
		stateEditor.putInt("hour_steps", 0);
		mCalendarNow = Calendar.getInstance();
		mCalendarNow.set(Calendar.MINUTE, 0);
		stateEditor.putLong("hour_time",mCalendarNow.getTimeInMillis());
		stateEditor.commit();
		
	}

	
}
